package com.example.course.controller;

import com.example.course.dto.CommentDTO;
import com.example.course.service.CommentService;
import com.example.course.tool.R;
import com.example.course.vo.CommentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 课程评论控制器
 *
 * @author Jasmine-drs
 * @date 2025-04-22
 */
@RestController
@RequestMapping("/api/comment")
@Api(tags = "课程评论接口")
public class CommentController {

	private final CommentService commentService;

	public CommentController(CommentService commentService) {
		this.commentService = commentService;
	}

	@GetMapping("/course/{courseId}")
	@ApiOperation("获取课程评论列表")
	public R<List<CommentVO>> getCommentsByCourseId(
			@ApiParam(value = "课程ID", required = true) @PathVariable Integer courseId) {
		try {
			List<CommentVO> commentVOList = commentService.getCommentsByCourseId(courseId);
			return R.success(commentVOList);
		} catch (Exception e) {
			return R.err("获取课程评论失败：" + e.getMessage());
		}
	}

	@PostMapping
	@ApiOperation("发表课程评论")
	public R<CommentVO> addComment(
			@ApiParam(value = "评论信息", required = true) @Valid @RequestBody CommentDTO commentDTO) {
		try {
			CommentVO commentVO = commentService.addComment(commentDTO);
			return R.success(commentVO);
		} catch (Exception e) {
			return R.err("发表评论失败：" + e.getMessage());
		}
	}
} 